import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import com.jchy.renderit.core.Camera;
import com.jchy.renderit.core.Light;
import com.jchy.renderit.core.MyInput;
import com.jchy.renderit.core.Point;
import com.jchy.renderit.core.PropertiesAndMethods;
import com.jchy.renderit.core.Displayable;
import com.jchy.renderit.displayable.BilliardStripe;
import com.jchy.renderit.displayable.Checked;
import com.jchy.renderit.displayable.Circle;
import com.jchy.renderit.displayable.Cube;
import com.jchy.renderit.displayable.Cylinder;
import com.jchy.renderit.displayable.Lens;
import com.jchy.renderit.displayable.Octahedron;
import com.jchy.renderit.displayable.Plane;
import com.jchy.renderit.displayable.Sphere;
import com.jchy.renderit.displayable.Tetrahedron;
import com.jchy.renderit.displayable.Triangle;


public class MainMenu implements ActionListener
{
	private Vector<Displayable> scene = new Vector<Displayable>();
	private Vector<Light> lights = new Vector<Light>();
	private Camera view1 = new Camera();
	private Camera view2 = new Camera();

	public Point prp1;
	public Point prp2;
	public int xMin,xMax,yMin,yMax;
	public Point lookat;
	public double invZoom;

	public void actionPerformed(ActionEvent e)
	{
		runMainMenu();
	}
	public void runMainMenu()
	{
		String mainMenu;
		String addType;
		String updtType;
		String updateThis;
		String delType;
		String deleteThis;
		Displayable aDisplayable;
		Light aLight;
		Camera aCamera;
		
		int pos;
        
		main_menu:
		do
		{
			aDisplayable=null;
			aLight=null;
			aCamera=null;
			pos=-1;
			addType="";
			updtType="";
			delType="";
			deleteThis="";
			System.out.print("(A)dd (U)pdate (D)elete (X)Exit: ");
			mainMenu=MyInput.readString();
			if(mainMenu.equals("a"))
			{
				System.out.print("(L)ight (D)isplayable (X)Exit: ");
				addType=MyInput.readString();
				if(addType.equals("l"))
				{
					aLight=new Light();
				} // Add Light
				else if(addType.equals("d"))
				{
					System.out.println("(P)lane (H)Checked (T)riangle (C)ircle ");
					System.out.println("(S)phere (Y)Cylinder (B)BilliardStripe (L)ens ");
					System.out.print("(U)Cube (R)Tetrahedron (O)ctahedron (X)Exit: ");
					String dispType=MyInput.readString();
					if(dispType.equals("p"))
						aDisplayable=new Plane();
					else if(dispType.equals("h"))
						aDisplayable=new Checked();
					else if(dispType.equals("t"))
						aDisplayable=new Triangle();
					else if(dispType.equals("c"))
						aDisplayable=new Circle();
					else if(dispType.equals("s"))
						aDisplayable=new Sphere();
					else if(dispType.equals("y"))
						aDisplayable=new Cylinder();
					else if(dispType.equals("b"))
						aDisplayable=new BilliardStripe();
					else if(dispType.equals("l"))
						aDisplayable=new Lens();
					else if(dispType.equals("u"))
						aDisplayable=new Cube();
					else if(dispType.equals("r"))
						aDisplayable=new Tetrahedron();
					else if(dispType.equals("o"))
						aDisplayable=new Octahedron();
					else continue main_menu;
               
				} // Add Displayable
				else continue main_menu;
               
			} // Add
			else if(mainMenu.equals("u"))
			{
				System.out.print("(L)ight (D)isplayable (V)iew (X)Exit: ");
				updtType=MyInput.readString();
				if(updtType.equals("v"))
				{
					updtview:
					for(int i=0;i<2; i++)
					{
						if(i==0)
						{
							System.out.println(view1.getClass().getName());
							System.out.println(view1);
						}
						else if(i==1)
						{
							System.out.println(view2.getClass().getName());
							System.out.println(view2);
						}
						
						System.out.print("Update this object? ");
						updateThis=MyInput.readString();
						if(updateThis.equals("y"))
						{
							if(i==0)
								aCamera=view1;
							else if(i==1)
								aCamera=view2;
							pos=i;
							break updtview;
						}
					}
				}
				else if(updtType.equals("l"))
				{
					updtlight:
					for(int i=0;i<lights.size();i++)
					{
						System.out.println(lights.elementAt(i).getClass().getName());
						System.out.println(lights.elementAt(i));
						System.out.print("Update this object? ");
						updateThis=MyInput.readString();
						if(updateThis.equals("y"))
						{
							aLight=lights.elementAt(i);
							pos=i;
							break updtlight;
						}
					} // traverse lights 
                    
					if(aLight==null)
						continue main_menu;
                  
				} // Update Light
				else if(updtType.equals("d"))
				{
					updtdisp:
					for(int i=0;i<scene.size();i++)
					{
						System.out.println(scene.elementAt(i).getClass().getName());
						System.out.println(scene.elementAt(i));
						System.out.print("Update this object? ");
						updateThis=MyInput.readString();
						if(updateThis.equals("y"))
						{
							aDisplayable=scene.elementAt(i);
							pos=i;
							break updtdisp;
						}
					} // traverse scene
					if(aDisplayable==null)
						continue main_menu;
                     
				} // Update Displayable
				else continue main_menu;
                  
			} // Update
			else if(mainMenu.equals("d"))
			{
				System.out.print("(L)ight (D)isplayable (X)Exit: ");
				delType=MyInput.readString();
				if(delType.equals("l"))
				{
					dellight:
					for(int i=0;i<lights.size();i++)
					{
						System.out.println(lights.elementAt(i));
						System.out.print("Delete this light? ");
						deleteThis=MyInput.readString();
						if(deleteThis.equals("y"))
						{
							aLight=lights.elementAt(i);
							pos=i;
							break dellight;
						}
					} // traverse lights 
                    
					if(aLight==null)
						continue main_menu;
                  
				} // Delete Light
				else if(delType.equals("d"))
				{
					deldisp:
					for(int i=0;i<scene.size();i++)
					{
						System.out.println(scene.elementAt(i).getClass().getName());
						System.out.println(scene.elementAt(i));
						System.out.print("Delete this object? ");
						deleteThis=MyInput.readString();
						if(deleteThis.equals("y"))
						{
							aDisplayable=scene.elementAt(i);
							pos=i;
							break deldisp;
						}
					} // traverse scene
					if(aDisplayable==null)
						continue main_menu;
                     
				} // Delete Displayable
				else continue main_menu;
                  
			} // Delete
			else break main_menu;
              
			String done="";
			if(!mainMenu.equals("d")) do
			{
				if(updtType.equals("l")) PropertiesAndMethods.prompt(aLight);
				else if(updtType.equals("d")) PropertiesAndMethods.prompt(aDisplayable);
				
				if(updtType.equals("l")) System.out.print("Are you done with this light? ");
				else if(updtType.equals("d")) System.out.print("Are you done with this object? ");
				
				done=MyInput.readString();
			
			} while(!done.equals("y"));
              
			if( mainMenu.equals("u") || mainMenu.equals("d") )
			{
				if(updtType.equals("l"))  lights.removeElementAt(pos);
				else if(updtType.equals("d"))  scene.removeElementAt(pos);

				if(delType.equals("l"))  lights.removeElementAt(pos);
				else if(delType.equals("d"))  scene.removeElementAt(pos);
			} // Update or Delete (then delete the object)
              
			if( mainMenu.equals("a") || mainMenu.equals("u") )
			{
				if(addType.equals("l"))  lights.addElement(aLight);
				else if(addType.equals("d"))  scene.addElement(aDisplayable);

				if(updtType.equals("l"))  lights.addElement(aLight);
				else if(updtType.equals("d"))  scene.addElement(aDisplayable);
			} // Add or Update (then add the object)
                            
			if( mainMenu.equals("u") && updtType.equals("v") )
			{
				if(pos==0)
					view1=(Camera)aCamera;
				else if(pos==1)
					view2=(Camera)aCamera;
				
			}

		} while(!mainMenu.equals("x"));
  
	}

}